package cn.net.xyan.nursing;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleInsets;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;

import static javax.swing.JFrame.EXIT_ON_CLOSE;

/**
 * Created by zarra on 16/12/13.
 */
public class CoreTemperatureChart {

    public JFreeChart createChart(XYDataset dataset) {

        JFreeChart chart = ChartFactory.createXYLineChart("Delaunay三角网构图",
                "X-坐标", "Y-坐标", dataset, PlotOrientation.VERTICAL, true, true,
                false);
        chart.setBackgroundPaint(Color.white);
        //chart.setPadding(new RectangleInsets(0,0,0,0));

        chart.setTitle( (String)null );
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setInsets(new RectangleInsets(0,0,0,0));

        plot.setAxisOffset(new RectangleInsets(0,0,0,0));

        /** 图例位置控制 */
        // chart.getLegend().setPosition(RectangleEdge.RIGHT);// 设置图例的位置
        chart.removeLegend();

        Stroke blackLine = new BasicStroke(1.5f);

        plot.setBackgroundPaint(Color.white);
        chart.setBorderStroke(blackLine);
        chart.setBorderPaint(Color.WHITE);
        plot.setDomainGridlineStroke(blackLine);
        plot.setRangeGridlineStroke(blackLine);
        plot.setDomainMinorGridlineStroke(new BasicStroke(1));
        plot.setRangeMinorGridlineStroke(new BasicStroke(1));

        plot.setDomainGridlinePaint(Color.BLACK); // 设置网格线
        plot.setDomainMinorGridlinesVisible(true);
        plot.setDomainMinorGridlinePaint(Color.LIGHT_GRAY);

        plot.setRangeGridlinePaint(Color.BLACK);
        plot.setRangeMinorGridlinesVisible(true);
        plot.setRangeMinorGridlinePaint(Color.LIGHT_GRAY);

        XYItemRenderer renderer = plot.getRenderer(); // renderer是指图形的绘图单元，绘图区

        // 线条形状，做图区域形状renderer
        XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) plot
                .getRenderer();
        // xylineandshaperenderer.setSeriesVisible(0,Boolean.FALSE);//某个系列是否可见
        // xylineandshaperenderer.setUseOutlinePaint(true);//数据点形状外轮廓
        xylineandshaperenderer.setLinesVisible(true);// 是否连线,XYLine中默认为true
        // xylineandshaperenderer.setShapesFilled(false);//数据点形状是否空心
        xylineandshaperenderer.setShapesVisible(true);// 数据点是否有形状,默认false

        // xylineandshaperenderer.setPaint(Color.BLACK);
        // Shape shape = new Ellipse2D.Float(0, 0, 3, 3);
         //xylineandshaperenderer.setShape(shape);//自定义一个新的数据形状

        /** 项目轴DomainAxis */
        NumberAxis numberaxis1 = (NumberAxis) plot.getDomainAxis(); // 获得横轴
        // numberaxis1.setAutoRange(true);
        numberaxis1.setAutoRangeIncludesZero(true);// 是否自动包含0起点? 默认为true
        numberaxis1.setTickUnit(new NumberTickUnit(1));
        numberaxis1.setMinorTickCount(5);
        numberaxis1.setLowerMargin(0);
        numberaxis1.setUpperMargin(0);
        //numberaxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        numberaxis1.setRange(0,7);

        numberaxis1.setVisible(false);
        // numberaxis1.setAutoRangeStickyZero(true);
        // numberaxis1.setRange(600330,800000);
        // numberaxis1.setLabelFont(new Font("宋体", 1, 18));

        /** 范围轴RangeAxis */
        NumberAxis numberaxis2 = (NumberAxis) plot.getRangeAxis(); // 获得纵轴
        numberaxis2.setAutoRangeIncludesZero(true);// 是否自动包含0起点? 默认为true
        numberaxis2.setTickUnit(new NumberTickUnit(1));
        numberaxis2.setMinorTickCount(5);
        numberaxis2.setLowerMargin(0);
        numberaxis2.setUpperMargin(0);
        //numberaxis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        numberaxis2.setRange(0,8);
        numberaxis2.setVisible(false);
        // numberaxis2.setAutoRange(true);
        // numberaxis2.setLabelFont(new Font("宋体", 1, 18));

        renderer.setSeriesPaint(0, Color.blue);// 更改颜色
        renderer.setSeriesPaint(1, Color.blue);
        renderer.setSeriesVisible(true);

        return chart;
    }

    public void show(XYDataset dataset) throws IOException {
        JFreeChart chart = createChart(dataset);

        ChartUtilities.saveChartAsJPEG(new File("output.jpg"),chart,1024,768);
        ChartFrame frame = new ChartFrame("Display", chart);
        frame.pack();

        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                frame.setVisible(true);
            }
        });
    }
}